package cn.bdqn.kaoshi2.controller;

import cn.bdqn.kaoshi2.pojo.Chat;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.security.Principal;

@Controller
public class GreetingController {


    @Resource
    SimpMessagingTemplate simpMessagingTemplate;

    /*@MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Message greeting(Message message){
        return  message;
    }*/

    /**
     * 点对点聊天
     * @param principal
     * @param chat
     */
    @MessageMapping("/chat")
    public void chat(Principal principal, Chat chat){
        String name = principal.getName();
        chat.setFrom(name);
        simpMessagingTemplate.convertAndSendToUser(chat.getTo(),"/queue/chat",chat);
    }

    @RequestMapping("/")
    public String index(){
        return "/onlinechat.html";
    }
}
